//
//  EdgeView.m
//  sdk2.0zhengquandasha
//
//  Created by Choi on 16/1/7.
//  Copyright © 2016年 palmaplus. All rights reserved.
//

#import "EdgeView.h"
#import "KeyWindowView.h"
#import "NewFuncIntroduceView.h"

@interface EdgeView()
//@property (nonatomic,assign)CGFloat currentX;
@property (nonatomic,assign)BOOL isPaning;
@property (nonatomic,strong)ChangeLocationStyleView *locationView;
@property (nonatomic,strong)NewFuncIntroduceView *fucnIntroduceView;
@end

@implementation EdgeView

- (IBAction)btnChangeStyle:(UIButton *)sender {
    NSLog(@"%ld",[[NSBundle mainBundle] loadNibNamed:@"ChangeLocationStyleView" owner:self options:nil].count);
    self.locationView = [[[NSBundle mainBundle] loadNibNamed:@"ChangeLocationStyleView" owner:self options:nil] lastObject];
    self.locationView.frame = CGRectMake(0, 44, self.width, self.height - 44);
    [self.locationView setCurrentLocationStyle:self.curStyle];
    __weak typeof(self)weakSelf = self;
    self.locationView.startLocationBlock = ^(LocationStyle style){
        if (weakSelf.startLocationBlock) {
            weakSelf.startLocationBlock(style);
        }
    };
    [self addSubview:self.locationView];
    self.labTitle.text = @"请选择定位方式";
}
- (IBAction)btnMyCollect:(UIButton *)sender {
}
- (IBAction)btnNewFunc:(UIButton *)sender {

    
    self.fucnIntroduceView = [[[NSBundle mainBundle] loadNibNamed:@"NewFuncIntroduceView" owner:self options:nil] lastObject];
    self.fucnIntroduceView.frame = CGRectMake(0, 44, self.width, self.height - 44);
    [self addSubview:self.fucnIntroduceView];
    self.labTitle.text = @"新功能介绍";
}

- (IBAction)btnBack:(UIButton *)sender {
        self.labTitle.text = @"设置";
    if (self.locationView) {
        [self.locationView removeFromSuperview];
        self.locationView = nil;
    }else if (self.fucnIntroduceView){
        [self.fucnIntroduceView removeFromSuperview];
        self.fucnIntroduceView = nil;
    }else {
        [KeyWindowView removeFromSuperView:0.5];
    }
}

-(instancetype)initWithCoder:(NSCoder *)aDecoder{
    if (self = [super initWithCoder:aDecoder]) {
        self.panGesture = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(handlePan:)];
        self.isPaning = NO;
        [self addGestureRecognizer:self.panGesture];
        [self addObserver:self forKeyPath:@"panGesture.state" options:NSKeyValueObservingOptionNew context:nil];
    }
    return self;
}

-(void)handlePan:(UIPanGestureRecognizer *)sender{
    CGPoint point = [sender translationInView:self];
    if (point.x > 0) {
        point.x = 0;
    }
    self.x = 0 + point.x;
    
}

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context{
    if ([keyPath isEqualToString:@"panGesture.state"]) {
      int temp =  [[change objectForKey:@"new"] intValue];
        if (temp == UIGestureRecognizerStateEnded) {
            NSLog(@"滑动结束");
            self.isPaning = NO;
            CGFloat temp = 0;
            CGFloat alpha = 0;
            if (self.x > -(self.width/3)) {
                temp = 0;
                alpha = 0.8;
            }else{
                temp = -self.width;
                alpha = 0;
            }
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.01 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//                            [UIView animateWithDuration:0.2 animations:^{
//                                NSLog(@"%f",temp);
//                                self.x = temp;
//                            }];
                [UIView animateWithDuration:0.3  animations:^{
                    self.x = temp;
                    [KeyWindowView showWithCurrentAlphaAnimation:0.3 toEndAlpah:alpha];
                } completion:^(BOOL finished) {
                    if (alpha == 0) {
                        [KeyWindowView removeFromSuperView:0];
                    }
                }];
                
            });
        }else if (temp == UIGestureRecognizerStateBegan){
            self.isPaning = YES;
            NSLog(@"开始滑动 %f",self.width);
        }
    }
}

-(void)removeFromSuperview{
    [self removeObserver:self forKeyPath:@"panGesture.state"];
    [super removeFromSuperview];
}

-(void)setFrame:(CGRect)frame{
    if (self.isPaning) {
        NSLog(@"fab = %f,x = %f,width = %f",0.8 - fabs(self.x/self.width),self.x,self.width);
        [KeyWindowView changeAlpha:0.8 - fabs(self.x/self.width)];
    }else{
        
    }
    [super setFrame:frame];
}



-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

}


@end
